#include "gameobject.h"
#include <QVector2D>
#include <QVector3D>
#include <QtCore/qmath.h>
#include <math.h>
#include <float.h> 


Rocket::Rocket(QString name)
	: GObject(name) 
{
	m_image.load(":/images/Rocket.png");
	//connect(, SIGNAL(collisionDetected()), this, SLOT(collisionRocket()));
}

Rocket::~Rocket()
{
}

QRectF Rocket::boundingRect() const
{
	return QRectF(0,0,m_image.width(),m_image.height());
}

void Rocket::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	Q_UNUSED(option)
    Q_UNUSED(widget)
	
	painter->drawPixmap(0, 0, m_image);
}

void Rocket::FinishedAnimation(QEvent::Type type, void* param)
{
	if(type == GEvent::Evt_MoveTo)
	{
		Destroy();
	}
}

void Rocket::CollisionDetected(void* param)
{
	Target* pTarget = static_cast<Target*>(GEngine::GetGame()->FindObject("Target"));
	
	//QList<QGraphicsItem*> collider = collidingItems(Qt::IntersectsItemShape);
	QList<QGraphicsItem*> collider = collidingItems(Qt::ContainsItemShape);
	QList<QGraphicsItem*>::Iterator itor;
	
	for(itor = collider.begin(); itor != collider.end(); ++itor )
	{
		QGraphicsItem* pItem = (*itor);
		if(pItem == pTarget)
		{
			StopAnimation();

			GEffect* pEffect = new GEffect("smoke");
			//pEffect->SetImage("images\\explode.png", 200, 210);
			pEffect->SetImage("images\\b_smoke.png", 64, 64);
			QPointF xxx(x(), y());
			GEngine::GetGame()->AddObject(pEffect, x(), y());
			StartAnimation(GEvent::Evt_Effect, pEffect->GetTotalFrame(), 1000, pEffect);
						
			Destroy();
			break;
		}
	}	
}

